<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <title>Apple圆角图片生成器</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <style>
            body {
                font-family: Arial, sans-serif;
                max-width: 800px;
                margin: 0 auto;
                padding: 20px;
            }
            .container {
                display: flex;
                flex-direction: column;
                gap: 20px;
            }
            canvas {
                max-width: 100%;
                border: 1px solid #ddd;
            }
            button {
                padding: 10px 15px;
                background: #007aff;
                color: white;
                border: none;
                border-radius: 5px;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>Apple圆角图片生成器</h1>
            <input
                type="file"
                id="imageInput"
                accept="image/png"
                title="apple"
            />
            <button id="downloadBtn" disabled>下载圆角图片</button>
            <canvas id="canvas"></canvas>
        </div>

        <script>
            const canvas = document.getElementById('canvas')
            const ctx = canvas.getContext('2d')
            const imageInput = document.getElementById('imageInput')
            const downloadBtn = document.getElementById('downloadBtn')

            let originalImage = null

            // 绘制超椭圆路径
            function drawSquircle(ctx, x, y, width, height, radius, n = 4) {
                const w = width / 2
                const h = height / 2

                ctx.beginPath()

                for (let i = 0; i <= 360; i++) {
                    const angle = (i * Math.PI) / 180
                    const cos = Math.cos(angle)
                    const sin = Math.sin(angle)

                    const rx =
                        Math.pow(Math.abs(cos), 2 / n) * w * Math.sign(cos) + w
                    const ry =
                        Math.pow(Math.abs(sin), 2 / n) * h * Math.sign(sin) + h

                    const px = x + rx
                    const py = y + ry

                    if (i === 0) {
                        ctx.moveTo(px, py)
                    } else {
                        ctx.lineTo(px, py)
                    }
                }

                ctx.closePath()
            }

            // 处理图片上传
            imageInput.addEventListener('change', function (e) {
                const file = e.target.files[0]
                if (!file) return

                const reader = new FileReader()
                reader.onload = function (event) {
                    originalImage = new Image()
                    originalImage.onload = function () {
                        // 设置canvas尺寸
                        canvas.width = originalImage.width
                        canvas.height = originalImage.height

                        // 计算圆角半径（取短边的10%作为半径）
                        const radius =
                            Math.min(
                                originalImage.width,
                                originalImage.height
                            ) * 0.1

                        // 绘制超椭圆蒙版
                        ctx.clearRect(0, 0, canvas.width, canvas.height)
                        drawSquircle(
                            ctx,
                            0,
                            0,
                            canvas.width,
                            canvas.height,
                            radius,
                            4
                        )
                        ctx.clip()

                        // 绘制原始图片
                        ctx.drawImage(originalImage, 0, 0)

                        downloadBtn.disabled = false
                    }
                    originalImage.src = event.target.result
                }
                reader.readAsDataURL(file)
            })

            // 下载处理后的图片
            downloadBtn.addEventListener('click', function () {
                if (!originalImage) return

                const link = document.createElement('a')
                link.download = 'apple-rounded-' + imageInput.files[0].name
                link.href = canvas.toDataURL('image/png')
                link.click()
            })
        </script>
    </body>
</html>
